using System;

namespace SharpMud.Net.Telnet.Session.DataProcessors
{
	/// <summary>
	/// Keeps statistical tabulations on data throughput
	/// </summary>
	public class DataThroughputTracker : System.IO.IDataProcessor
	{
		#region Private Variables
		private System.Collections.IQueue<byte> _InputQueue;
		private System.Collections.IQueue<byte> _OutputQueue;

		private System.DateTime							_LastThroughputTime;
		private long									_TotalThroughput;
		#endregion
		
		#region Constructors
		public DataThroughputTracker()
		{
			_TotalThroughput = 0;
			_LastThroughputTime = System.DateTime.MinValue;
		}
		#endregion

		#region Public Properties
		public long TotalThroughput
		{
			get
			{
				return _TotalThroughput;
			}
		}
		public System.DateTime LastThroughputTime
		{
			get
			{
				return _LastThroughputTime;
			}
		}
		#endregion

		#region IDataProcessor Members

		public System.Collections.IQueue<byte> OutputQueue
		{
			get
			{
				return _OutputQueue;
			}
		}

		public System.Collections.IQueue<byte> InputQueue
		{
			get
			{
				return _InputQueue;
			}
		}

		public void Initialize(System.Collections.IQueue<byte> inputQueue, System.Collections.IQueue<byte> outputQueue)
		{
			_InputQueue = inputQueue;
			_OutputQueue = outputQueue;
		}

		public void Pulse()
		{
			if(this._InputQueue.Count>0)
			{
				lock(InputQueue)
				{
					lock(OutputQueue)
					{
						lock(this)
						{
							int c = _InputQueue.Count;
							this._TotalThroughput = this._TotalThroughput + c;
							this._OutputQueue.Enqueue(_InputQueue.Dequeue(c));
							this._LastThroughputTime = System.DateTime.Now;
						}
					}
				}
			}
		}

		#endregion

        #region IDisposable Members

        public void Dispose()
        {
            //Nothing to dispose
        }

        #endregion
    }
}
